package com.menggudf.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.menggudf.dao.LinkAricleDao;
import com.menggudf.entity.LinkAricle;
import com.menggudf.model.LinkAricleModel;
import com.menggudf.model.ResultModel;
import com.menggudf.service.LinkAricleService;

@Service
public class LinkAricleServiceImpl implements LinkAricleService {

	private static final Logger LOGGER = LoggerFactory
			.getLogger(LinkAricleServiceImpl.class);
	
   @Autowired
   private LinkAricleDao linkAricleDao;
	
	@Override
	@Transactional(value = "medicalTM", rollbackFor=Exception.class)
	public ResultModel toSaveLinkAricle(String title,String linkAricle,String organization, String source,String user) {
		try {
			LinkAricle linkAricles=new LinkAricle();
			linkAricles.setCreator(user);
			linkAricles.setLinkAricle(linkAricle);
			linkAricles.setOrganization(organization);
			linkAricles.setSource(source);
			linkAricles.setStartTime(new Date());
			linkAricles.setTitle(title);
			linkAricleDao.persist(linkAricles);
			return ResultModel.success();
		} catch (Exception e) {
			LOGGER.error("toSaveLinkAricle fail",e);
		}
		return ResultModel.fail();
	}

	@Override
	public Map<String, Object> queryAllAricleList() {
		StringBuffer sql=new StringBuffer();
		sql.append("select * from linkaricle ");
		return null;
	}

    @Override
    @Transactional(value = "medicalTM", readOnly=true)
    public Map<String, Object> getLinkAricleList(
            String title,
            String source,
            Date startTime,
            Date endTime,
            int pageSize,
            int offset) {
        List<LinkAricle> list = linkAricleDao.getLinkAricleList(title,source,startTime,endTime,pageSize,offset);
        long count = linkAricleDao.getLinkAricleListCount(title,source,startTime,endTime);
        List<LinkAricleModel> mlist = new ArrayList<LinkAricleModel>();
        for(LinkAricle l : list){
            LinkAricleModel ml = new LinkAricleModel();
            ml.setId(l.getId());
            ml.setCreator(l.getCreator());
            ml.setLinkAricle(l.getLinkAricle());
            ml.setOrganization(l.getOrganization());
            ml.setSource(l.getSource());
            ml.setTitle(l.getTitle());
            ml.setUpdateAuthor(l.getUpdateAuthor());
            ml.setUpdateTime(l.getUpdateTime());
            ml.setStartTime(l.getStartTime());
            mlist.add(ml);
        }
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("total", count);
        map.put("rows", mlist);
        return map;
    }

    @Override
    @Transactional(value = "medicalTM", rollbackFor=Exception.class)
    public ResultModel deleteLinkAricle(Integer id) {
        LinkAricle LinkAricle = linkAricleDao.findById(id);
        if(LinkAricle==null){
            return ResultModel.fail();
        }
        try {
            linkAricleDao.remove(LinkAricle);
            return ResultModel.success();
        } catch (Exception e) {
            LOGGER.error("deleteLinkAricle fail",e);
        }
        return ResultModel.fail();
    }

	

}
